package cn.dslcode.security.common.util;

import cn.dslcode.security.common.SystemUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 用户工具类
 */
public class UserUtil {
    /*
     * 获取当前用户
     * 
     * @return
     */
    public static SystemUser getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal != null && principal instanceof SystemUser) {
                return (SystemUser) principal;
            }
        }
        return null;
    }
}